<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"></c:set>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <title>软件管理平台</title>
    <meta name="description" content="">
    <meta name="keywords" content="">
    <meta name="viewport" content="width=device-width">
    <link href="${ctx}/statics/css/common/common.css" rel='stylesheet'/>
    <link href="${ctx}/statics/css/font-awesome-4.5.0/css/font-awesome.css" rel="stylesheet"/>
    <link rel="stylesheet" type="text/css" href="${ctx}/statics/assets/jquery-Upload/css/default.css">
    <link href="${ctx}/statics/assets/jquery-Upload/css/fileinput.css" media="all" rel="stylesheet" type="text/css" />
    <link href="${ctx}/statics/assets/bootstrap/css/bootstrap.min.css" rel="stylesheet"/>

    <script src="${ctx}/statics/assets/jquery/jquery-3.1.0.min.js"></script>
    <script src="${ctx}/statics/assets/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
    <script src="${ctx}/statics/assets/jquery-Upload/js/fileinput.js" type="text/javascript"></script>
    <script src="${ctx}/statics/assets/jquery-Upload/js/fileinput_locale_zh.js" type="text/javascript"></script>
    <script src="${ctx}/statics/assets/layui-layer-v2.4/layer.js" type="text/javascript"></script>
    <script src="${ctx}/statics/js/upload.js"></script>

</head>
<body>
<div class="container">
    <div class="row upload-row-content">
        <form id="addForm" role="form" action="${ctx}/software/add" method="post" enctype="multipart/form-data">
            <div class="form-group">
                <div class="col-md-2 text-left" style="font-weight: bold">软件名称：</div>
                <div class="col-md-10">
                    <input type="text" id="softwareName" name="name" class="form-control" multiple style="margin-bottom: 0.5rem" placeholder="请输入软件名称..." >
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-2 text-left" style="font-weight: bold">类型：</div>
                <div class="col-md-10" style="margin-bottom: 0.5rem">
                    <select id="categoryId" name="categoryId" class="form-control">
                        <%--<option value="">常用软件</option>
                        <option value="">开发软件</option>--%>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-2 text-left" style="font-weight: bold">图标：</div>
                <div class="col-md-10" style="margin-bottom: 0.5rem">
                    <input id="file-icon" name="softIcon" type="file" class="file" >
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-2 text-left" style="font-weight: bold">软件简介：</div>
                <div class="col-md-10">
                    <textarea id="softwareDesc" name="description" class="form-control" style="margin-bottom: 0.5rem;" rows="5" placeholder="请输入软件简介..." ></textarea>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-2 text-left" style="font-weight: bold">文件：</div>
                <div class="col-md-10">
                    <form id="fileupload" action="${ctx}/softwareUpload/uploadFile" enctype="multipart/form-data">
                        <input id="file-software" type="file" name="softwarefile" class="file">
                    </form>
                    <input type="hidden" id="downloadUrl" name="downloadUrl" value=""/>
                    <input type="hidden" name="size" value=""/>
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-12 text-center" style="margin-top: 1.5rem"><submit id="uploadSubmit" class="btn btn-primary" >提交</submit></div>
            </div>
        </form>
    </div>
</div>
<script>
    $("#file-icon").fileinput({
        'uploadUrl': '',
        'maxFileSize': 10000,
        'maxFileCount': 1,
        'showRemove': true,
        'showUpload': false,
        'showCancel': false,
        'showPreview': false
    });

    $("#file-software").fileinput({
        'uploadUrl': '${ctx}/softwareUpload/uploadFile',
        'maxFileSize': 1073741824,
        'maxFileCount': 1,
        'showPreview': false,
        'ajaxSettings': {
            'url': '${ctx}/softwareUpload/uploadFile',
            'success': function(result){
                console.log(result);
                $("input[name='downloadUrl']").val(result.filePath);
                $("input[name='size']").val(result.fileSize);
            }
        }
    });

    $.ajax({
        url: '${ctx}/softwarecategory/get',
        dataType: 'json',
        success: function(data) {
            $.each(data, function(index, content) {
                var opt = '<option value="' + content.id  + '">' + content.name + '</option>'
                $("#categoryId").append($(opt));
            });
        }
    });

    $("#uploadSubmit").click(function() {
        $("#addForm").submit();
    });

    $("#addForm").submit(function(){
        console.log($("#softwareName").val() == '');
        console.log($("#softwareDesc").val() == '');
        if($("#softwareName").val() == ''){
            layer.tips('请填写软件名称！', '#softwareName', {
                tips: [4, '#FF9900'],
                time: 3000
            });
            return false;
        }
        if($("#softwareDesc").val() == ''){
            layer.tips('请填写描述信息！', '#softwareDesc', {
                tips: [4, '#FF9900'],
                time: 3000
            });
            return false;
        }
        if($("#downloadUrl").val() == ''){
            layer.tips('请上传文件！', '#file-software', {
                tips: [2, '#FF9900'],
                time: 3000
            });
            return false;
        }

        return true;
    });

</script>
</body>
</html>